import { isMobile, isPassword } from '@/utils'

const isPwd = val => val.length >= 6
// 表单验证
const form = {
  data () {
    // 冲突时以组件数据优先
    return {

    }
  },
  // 同名钩子函数都调用，混入的在前
  mounted () {

  },
  // methods、components 和 directives 合并为一个，冲突时以组件优先
  methods: {
    // 收起键盘
    handleConfirm () {
      uni.hideKeyboard()
    },

    handleInputChange (key, e) {
      this.form[key] = e.detail.value
    },

    handleCloseClick (key) {
      this.form[key] = ''
    },

    // 非空验证
    required (val) {
      return Boolean(val.trim().length)
    },

    // 过滤空格
    formatter (value) {
      return value.replace(/\s/g, '')
    },

    // 手机号验证
    handleMobileBlur () {
      if (!isMobile(this.form.mobile)) {
        this.errorMessage.mobile = '请输入正确的手机号'
      } else {
        this.errorMessage.mobile = ''
        return true
      }
    },

    // 验证码验证
    handleSMSCodeBlur (val) {
      if (!/^\d{6}$/.test(this.form.smsCode)) {
        this.errorMessage.smsCode = '请输入6位验证码'
      } else {
        this.errorMessage.smsCode = ''
        return true
      }
    },

    // 登录密码验证
    handlePasswordBlur () {
      if (!isPwd(this.form.password)) {
        // this.errorMessage.password = '密码至少6位，包含字母和数字'
        this.errorMessage.password = '密码至少6位'
      } else {
        this.errorMessage.password = ''
        return true
      }
    },

    handlePasswordConfirmBlur () {
      if (!isPassword(this.form.confirmPassword)) {
        this.errorMessage.confirmPassword = '密码至少6位，包含字母和数字'
      } else if (isPassword(this.form.password) && (this.form.password !== this.form.confirmPassword)){
        this.errorMessage.confirmPassword = '两次输入的密码不一致'
      } else {
        this.errorMessage.confirmPassword = ''
        return true
      }
    }

  }
}

export default form
